/*
 * @Date: 2022-03-07 14:07:01
 * @LastEditTime: 2022-03-17 12:24:17
 */
import go2 from './utils/go2';
import api from './api/index';

// app.js
App({
  async onLaunch() {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，请重启应用',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              },
            });
          });
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '升级失败',
              content: '新版本下载失败，请检查网络！',
              showCancel: false,
            });
          });
        }
      });
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      });
    }
    this.setGlobaFunction();
    this.getSystemInfo();
    this.initCloudFunction();
    wx.cloud.init({
      traceUser:true
    })
    wx.cloud.callFunction({
      name:"getOpenid",
      success:res=>{
        console.log("res:",res.result.openid);
        let app = getApp();
        app.globalData.openId = res.result.openid;
      }
    })
    this.globalData = {
      chooseTags:[[],[],[]]
    }
  },
  setGlobaFunction() {
    wx.$go2 = go2;
    wx.$api = api;
  },
  getSystemInfo() {
    wx.getSystemInfo({
      success: (res) => {
        const {
          screenHeight,
          safeArea
        } = res;
        const bottomSafe = screenHeight - safeArea.bottom;
        wx.setStorageSync('bottomSafe', bottomSafe);
      },
    });
  },
  initCloudFunction() {
    return new Promise((resolve,reject)=>{
      if(wx.$cloud)
        resolve(wx.$cloud)
      else{
        var oldCloud = new wx.cloud.Cloud({
          // 资源方 AppID
          resourceAppid: 'wxf5e4826a10e37e11',
          // 资源方环境 ID
          resourceEnv: 'cloud1-8ggeumhe5b48fafe',
        })
        oldCloud.init().then(()=>{
          wx.$cloud = oldCloud
          resolve(oldCloud)
        })
      }
    })
  },
  globalData: {
    currentPos: {
      city: "广州市",
      latitude: 23.12908,
      longitude: 113.26436,
      adcode: "440104"
    },
    openId:"",
    mobile: "",
    chooseTags: {}
  },
  
});